package com.magist.manage.web.controller;

import com.github.pagehelper.PageInfo;
import com.magist.core.controller.BaseController;
import com.magist.core.entity.ResultMessage;
import com.magist.core.exception.BusinessException;
import com.magist.core.util.CodeUtils;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;
import tk.mybatis.mapper.entity.Example;


import com.magist.manage.web.entity.SysAuth;
import com.magist.manage.web.dto.SysAuthDto;
import com.magist.manage.web.service.SysAuthService;

@ApiIgnore
@Api(value = "RestSysAuthController", description = "RestSysAuthController接口")
@RestController
@RequestMapping("/rest/sysAuth")
public class RestSysAuthController extends BaseController {

	@Autowired
	private SysAuthService sysAuthService;

	@ApiOperation(value="保存", notes="保存接口")
	@RequestMapping(value = "save.do", method = RequestMethod.POST)
	public Object save(@ModelAttribute("sysAuthDto") SysAuthDto sysAuthDto) {
		SysAuth sysAuth = new SysAuth();
		BeanUtils.copyProperties(sysAuthDto,sysAuth);
		sysAuth.setId(sysAuthService.getNewId());
		sysAuth.setCode(CodeUtils.idToCode(sysAuth.getId(),4));
		sysAuthService.addAuth(sysAuth);
		return ResultMessage.success();
	}

	@ApiOperation(value="更新", notes="更新接口")
	@RequestMapping(value = "update.do", method = RequestMethod.POST)
	public Object update(@ModelAttribute("sysAuthDto") SysAuthDto sysAuthDto) {
		SysAuth oldSysAuth = sysAuthService.getById(sysAuthDto.getId());
		if(oldSysAuth==null)
			throw new BusinessException("无效的数据!");
		ResultMessage result =null;
		SysAuth sysAuth = new SysAuth();
		BeanUtils.copyProperties(sysAuthDto,sysAuth);
		sysAuth.setModifyTime(oldSysAuth.getModifyTime());
		if (sysAuthService.update(sysAuth)>0) 
			result = ResultMessage.success("操作成功");
		else
			result=ResultMessage.error("操作失败!");
		return result;
	}

	@ApiOperation(value="列表", notes="分页查询接口")
	@RequestMapping(value = "page.do", method = RequestMethod.GET)
	public Object page(@ModelAttribute("sysAuthDto") SysAuthDto sysAuthDto) {
		ResultMessage result = ResultMessage.success();
		SysAuth sysAuth = new SysAuth();

		if(StringUtils.isNotBlank(sysAuthDto.getName()))
			sysAuth.setName("%"+sysAuthDto.getName().trim()+"%");
		if(StringUtils.isNotBlank(sysAuthDto.getCode()))
			sysAuth.setCode("%"+sysAuthDto.getCode().trim()+"%");
		if(StringUtils.isNotBlank(sysAuthDto.getUrl()))
			sysAuth.setUrl("%"+sysAuthDto.getUrl().trim()+"%");
		PageInfo<SysAuth> pageInfo = sysAuthService.page(sysAuth,getPageInfo());
		result.setContent(pageInfo);
		return result;
	}

	@ApiOperation(value="删除", notes="删除接口")
	@RequestMapping(value = "del.do", method = RequestMethod.POST)
	public Object del(@ModelAttribute("id") String id) {
		SysAuth sysAuth = sysAuthService.getById(id);
		if(sysAuth==null)
			throw new BusinessException("无效的数据!");
		ResultMessage result =null;
		if(sysAuthService.delete(sysAuth)>0)
			result = ResultMessage.success("操作成功");
		else
			result=ResultMessage.error("操作失败!");
		return result;
	}

	@ApiOperation(value="详情", notes="详情接口")
	@RequestMapping(value = "info.do", method = RequestMethod.GET)
	public Object info(@ModelAttribute("id") String id) {
		SysAuth sysAuth = sysAuthService.getById(id);
		if(sysAuth==null)
			throw new BusinessException("无效的数据!");
		SysAuthDto sysAuthDto = new SysAuthDto();
		BeanUtils.copyProperties(sysAuth,sysAuthDto);
		ResultMessage result = ResultMessage.success();
		result.setContent(sysAuthDto);
		return result;
	}

	private Example packExample(Example example,SysAuthDto sysAuthDto){
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("deleted","F");
		if(StringUtils.isNotEmpty(sysAuthDto.getId()))
			criteria.andEqualTo("id",sysAuthDto.getId());
		return example;
	}
}
